@extends('layouts.common')

@section('title')权限清单@endsection

@section('content')
    <div id="toolbar">
        {!! $toolBarButton !!}
    </div>

    <div id="listGrid"></div>
    <div id="form_dialog"></div>
    <input type="hidden" id="raw_id" name="raw_id" value="">
@endsection

@section('js')
    <script type="text/javascript">
        $(function () {
            $('#listGrid').datagrid({
                url: '/sys/user/powerlist',
                rownumbers: true,
                pagination: true,
                title: '权限列表',
                toolbar: '#toolbar',
                fit: true,
                singleSelect:"true",
                queryParams:{
                    _token : $('meta[name="csrf-token"]').attr('content'),
                },

                columns:[[
                    { field:'id',checkbox:true},
                    { field:'code',title:'编码',width:80,align:'center'},
                    { field:'name',title:'名称',width:100,align:'center'},
                    { field:'description',title:'描述',width:150,align:'center'},
                    { field:'is_active',title:'有效',width:80, formatter: format_active},
                ]],
                onClickRow:function (index,raw) {
                    $("input[name='raw_id']").val(raw.id);
                }
            });
        });

        function openAdd(){
            $('#form_dialog').dialog({
                title: '添加信息',
                width: 400,
                height: 200,
                closed: false,
                cache: false,
                href: '/sys/user/poweradd',
                modal: true,
                buttons: [{
                    text: '确定',
                    iconCls: 'icon-ok',
                    handler: save
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $('#form_dialog').dialog('close');
                    }
                }]
            });
        }

        function openEdit(){
            var raw_id = $("input[name='raw_id']").val();
            if (raw_id === '') {
                $.messager.alert('警告','请选择一行');
            }else{
                $('#form_dialog').dialog({
                    title: '修改信息',
                    width: 400,
                    height: 200,
                    closed: false,
                    cache: false,
                    href: '/sys/user/poweredit?id='+raw_id,
                    modal: true,
                    buttons: [{
                        text: '确定',
                        iconCls: 'icon-ok',
                        handler: save
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#form_dialog').dialog('close');
                        }
                    }]
                });
            }

        }

        function save(){
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url:'/sys/user/powersave',
                type:'post',
                data:$('#info-form').serialize(),
                dataType:'json',
                success:function (data) {
                    if(data.code === 200){
                        $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')})
                        $('#form_dialog').dialog('close');
                    }else{
                        $.messager.alert("提示信息", data.data , "error");
                    }
                }
            });
        }

        function remove(){
            var raw_id = $("input[name='raw_id']").val();
            if (raw_id === '') {
                $.messager.alert('警告','请选择一行');
            }else {
                $.messager.confirm('信息提示','确定要删除该记录？', function(result){
                    if(result){

                        $.ajax({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            type:'post',
                            url:'/sys/user/powerremove',
                            data:{
                                id:raw_id
                            },
                            success:function(data){
                                if(data.code === 200){
                                    $.messager.alert('信息提示','删除成功！');
                                    $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')});
                                }
                                else {
                                    $.messager.alert('信息提示','删除失败,请稍后再试！');
                                    $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')})
                                }
                            }
                        });
                    }
                });
            }

        }
    </script>
@endsection